package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.dao.impl.NavImpl;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import com.mysql.cj.util.StringUtils;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NavServiceImpl implements INavService {
    @Override
    public List<NavModel> getNavModelListForHome() {
        /**
         * 1.判断缓存里面是否有数据
         * 2.有，则取缓存
         * 3.没有则取数据库
         */
        Jedis jedis = new Jedis();
        List<NavModel> list = new ArrayList<>();
        Boolean exists = jedis.exists("navHref");
        if (exists) {
            long leng = jedis.llen("navHref");
            for (int i = 0; i < leng; i++) {
                NavModel model = new NavModel();
                model.setHref(jedis.lindex("navHref", i));
                model.setTitle(jedis.lindex("navTitle", i));
            }
        } else {
            NavDao navDao = new NavImpl();
            list = navDao.getNavModelListForHome();
            for (NavModel navModel : list) {
                jedis.rpush("navHref", navModel.getHref());
//                jedis.expire("navHref", 10);
                jedis.rpush("navTitle", navModel.getTitle());
//                jedis.expire("navTitle", 10);
            }

        }
        return list;

    }
//    public int  update(NavModel navModel){
//        NavDao navDao = new NavImpl();
//        int result=navDao.update(navModel);
//        if(result==1){
//            Jedis jedis=new Jedis();
//            jedis.del("navHref","navTitle");
//        }
//        return 0;
//    }
}
